﻿//*****************************************************************************
//
//  Silver Games - Checkers
//  Copyright © 2009 Randi Relander
//      <rjrelander@users.sourceforge.net>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//*****************************************************************************

using System.Collections.Generic;

namespace Checkers.Common
{
    /// <summary>
    /// Move list class.
    /// </summary>
    public class MoveList : List<Move>
    {
        /// <summary>
        /// Adds move to list.
        /// </summary>
        /// <param name="checker">Checker that is moving.</param>
        /// <param name="direction">Direction of move.</param>
        public void AddMove(Checker checker, Direction direction)
        {
            Move move = new Move();

            move.Tile1 = checker.GetNextTile(direction, 0);
            move.Tile2 = checker.GetNextTile(direction, 1);

            Add(move);
        }

        /// <summary>
        /// Adds jump to list.
        /// </summary>
        /// <param name="checker">Checker that is jumping.</param>
        /// <param name="direction">Direction of jump.</param>
        public void AddJump(Checker checker, Direction direction)
        {
            Move move = new Move();

            move.Tile1 = checker.GetNextTile(direction, 0);
            move.Tile2 = checker.GetNextTile(direction, 2);

            move.Jump = checker.GetNextTile(direction, 1);

            Add(move);
        }

        /// <summary>
        /// Finds move that matches given tiles.
        /// </summary>
        /// <param name="tile1">Start tile.</param>
        /// <param name="tile2">End tile.</param>
        /// <returns>Move that that matches given tiles.</returns>
        public Move FindMove(Tile tile1, Tile tile2)
        {
            foreach (Move move in this)
            {
                if ((move.Tile1 == tile1) && (move.Tile2 == tile2))
                {
                    return move;
                }
            }

            return null;
        }

        /// <summary>
        /// Checks if list has move staring at given tile.
        /// </summary>
        /// <param name="tile">Start tile.</param>
        /// <returns>True if list has move staring at given tile.</returns>
        public bool HasStartTile(Tile tile)
        {
            foreach (Move move in this)
            {
                if (move.Tile1 == tile)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Appends move list to list.
        /// </summary>
        /// <param name="moveList">Move list to append.</param>
        public void Append(MoveList moveList)
        {
            foreach (Move move in moveList)
            {
                Add(move);
            }
        }
    }
}
